package com.wn.trade.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wn.common.model.dto.BorrowRecordDTO;
import com.wn.common.result.Result;
import com.wn.trade.bean.BorrowRecord;
import com.wn.trade.service.IBorrowRecordService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yellow Docter
 * @date 2023 -07-31
 * @desc
 */
@RestController
public class TradeController {

    @Autowired
    private IBorrowRecordService borrowRecordService;

    @ApiOperation("借款申请...")
    @PostMapping("/auth/trade/borrow")
    public String borrowRecord(@RequestBody BorrowRecordDTO borrowRecordDTO) {

        //进行 借款
        borrowRecordService.borrow(borrowRecordDTO);
        return "借款成功...";

    }

    @ApiOperation("查询借款记录")
    @GetMapping("/auth/trade/search")
    public Result searchborrowRecord(String name, String createDate) {

        LambdaQueryWrapper<BorrowRecord> borrowRecordLambdaQueryWrapper = Wrappers.lambdaQuery(BorrowRecord.class);

        if (StringUtils.isNotBlank(name)) {
            borrowRecordLambdaQueryWrapper.like(BorrowRecord::getName, name);
        }
        if (StringUtils.isNotBlank(createDate)) {
            borrowRecordLambdaQueryWrapper.gt(BorrowRecord::getCreateDate, createDate);
        }

        List<BorrowRecord> list = borrowRecordService.list(borrowRecordLambdaQueryWrapper);

        return Result.ok("查询成功", list);
    }

    @ApiOperation("借款记录审核")
    @GetMapping("/auth/trade/status/{borrowId}/{status}")
    public String tradeChangeStatus(
            @PathVariable("borrowId") Long borrowId,
            @PathVariable("status") Integer status) {
        String result = borrowRecordService.changeStatus(borrowId, status);
        return result;
    }

    @ApiOperation("机构认购投资理财产品")
    @PostMapping("/auth/trade/agency/bid/add/{borrowId}/{amount}")
    public Result addAgencyBid(
            @PathVariable("borrowId") Long borrowId,       //借款的id
            @PathVariable("amount") Integer amount) {      //投机的金额

        //投资认证流程
        Map<String, Object> map = borrowRecordService.addAgencyBid(borrowId, amount);
        return  Result.ok(map);
    }

    @ApiOperation("机构认购投资的审核")
    @PostMapping("/trade/agency/bid/status")
    public String agencyBidStatus(
            @RequestParam("borrowId")Long borrowId,         //借款id
            @RequestParam("commission")String commission,   //平台佣金
            @RequestParam("status")String status            //审核状态
    ){

       String result =  borrowRecordService.agencyBidStatus(borrowId,commission,status);
       return result;

    }
}
